<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <link rel="stylesheet" href="../../layui/css/formSelects-v4.css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery.min.js"></script>
    <script type="text/javascript" src="../../js/vue.js"></script>
</head>
<body>
<div id="story">
<h2>班级维护</h2>
<!--<h1>{{garden_list.glName}}</h1>-->
    <input type="hidden" name="glId" id="glId" :value="garden_list.glId">
    <input type="hidden" name="gIId" id="gIId" :value="garden_list.glId">
    <input type="hidden" :value="garden_list.glName" id="gradeName">
<table class="layui-hide" id="manageList" lay-filter="manageList"></table>
</div>
<!--<script>
    $.get("http://localhost:9999/Manage/all",function(data){
        $.each(data,function(){
            var opt = $("<option></option>").appendTo("#roles");
            opt.text(this.roleName).val(this.roleId);
        });
        /* layui.formSelects.render();*/
    });
</script>-->
<!--<script src="../../js/config.js"></script>-->
<script>
    var table ;
/*    function reloadData(){
        var roleIds = layui.formSelects.value('roles','val');
        alert(roleIds);
        //执行重载
        table.reload('userList', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                userName : $("#username").val(),
                "cha":roleIds.join(",")
            }
        });
    }*/

    layui.config({
        base: '../../layui/'
    }).extend({
        formSelects: 'formSelects-v4'
    });

    layui.use(['table','form','formSelects'], function(){
        table = layui.table;
        var form = layui.form;
        var select = layui.formSelects;

        table.render({
            elem: '#manageList'
            ,url:'http://localhost:9999/Manage/manageIndex?glId='+$("#glId").val()
           /* ,page: true*/
            ,even : true
            ,id : "manageList"
            ,toolbar:"#toolbarDemo",
            defaultToolbar: []
            ,cols: [[
                {field:'hello',type:'checkbox'}
                ,{field:'cmId', title: '编号'}
                ,{field:'cmName', title: '班级名称'}
                ,{field:'cmNumber',  title: '班级人数', sort: true}
                ,{field:'grade',  title: '所属年级',templet:function (d) {
                        return d.grade.gradeName;
                    }}
               ,
                {toolbar: '#barDemo',title:'操作'}
            ]]
        });

        //监听行内的工具条
        table.on('tool(manageList)',function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        console.log(data);
            if(layEvent === 'del'){ //删除
                layer.confirm('确认删除吗？', function(index){
                    layer.close(index);
                    $.post("http://localhost:9999/Manage/manageDelete",{cmId:data.cmId},function(d){
                        table.reload("manageList");

                    });
                });
            }else if (layEvent === 'edit'){//TODO 编辑
                $.get('updcla.html?_'+new Date().getTime(),function (s) {
                    layer.open({
                        type : 1,
                        title : '班级维护',
                        area:'800px',
                        content:s,
                        anim:2,
                        btn : ['确定','取消'],
                        success:function(){
                            //对表单回填数据
                           /* var grades = [];
                            grades.push(data.grade.gradeId)*/
                            form.val('manageForm',data);
                            var time = setInterval(function () {
                                if ($("#grade option").length > 1) {
                                    $("#grade").val(data.grade.gradeId);
                                    layui.form.render();
                                    clearInterval(time);
                                }
                            },100)
                            var time2 = setInterval(function () {
                                if ($("#glIdName").val() != ""){
                                    $("#glIdName").val($("#gradeName").val());
                                    layui.form.render();
                                    clearInterval(time2);
                                }
                            },100)
                            var time3 = setInterval(function () {
                                if ($("#glId").val() != ""){
                                    $("#glId").val($("#glId").val());
                                    layui.form.render();
                                    clearInterval(time3);
                                }
                            })
                        },
                        yes : function () {
                            var str = $("#add_manage_form").serialize();
                            console.log(str);
                            $.get("http://localhost:9999/Manage/manageUpdate",str,function () {
                                layer.close(layer.index);
                                table.reload('manageList');
                            })
                        }
                    })
                })
            } else if (layEvent === 'detail'){ //TODO 查看详情

            }
        });
        //头工具栏事件
        table.on('toolbar(manageList)', function(obj){
            var h = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'batchDelete': //批量删除
                   /* var data = h.data;
                    if(data.length==0){
                        layer.msg("请选择要删除的内容");
                        return;
                    }
                    layer.confirm("要删除吗",function () {
                        var shan = "?";
                        for(let i of data){
                            shan+=`id=${i.userId}&`;
                        }
                        $.get(`http://localhost:9999/kmsuser/batchDelete${shan}`,function () {
                            layer.close(layer.index);
                            table.reload('userList');
                        });
                    });*/

                    break;
                case 'add':
                    $.get("addcla.html",function (data) {
                        layer.open({
                            type : 1,
                            title : '添加班级',
                            skin : 'layui-layer-molv',
                            anim:3,
                            area : '800px',
                            content : data,
                            btn : ['确定','取消'],
                            success:function(){
                                //对表单回填数据
                                var time2 = setInterval(function () {
                                    if ($("#glIdName").val() != ""){
                                        $("#glIdName").val($("#gradeName").val());
                                        layui.form.render();
                                        clearInterval(time2);
                                    }
                                },100)
                                var time3 = setInterval(function () {
                                    if ($("#glId").val() != ""){
                                        /*alert("sdf");*/
                                        $("#glIdadd").val($("#glId").val());
                                        alert($("#glIdadd").val());
                                        layui.form.render();
                                        clearInterval(time3);
                                    }
                                })


                            },
                            yes : function () {
                                var str = $("#add_manage_form").serialize();
                                $.get("http://localhost:9999/Manage/manageAdd",str,function () {
                                    layer.close(layer.index);
                                    table.reload('manageList');
                                });
                            }
                        })

                    });
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });

    });



    var vm = new Vue({
        el:'#story',
        data:{
            garden_list:null
        },
        created:function () {
            var User_PermissionData = JSON.parse(sessionStorage.getItem("login_user"));
            this.garden_list = User_PermissionData.kmsUser.garden_list;
            /*if (a == null){
                this.garden_listName = '总部';
            } else {
                this.garden_listName=a.glName;
            }*/
        },
    })
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i> 添加</button>
        <!--<button class="layui-btn layui-btn-sm" lay-event="batchDelete"><i class="layui-icon layui-icon-delete"></i> 批量删除</button>-->
    </div>
</script>

<script type="text/html" id="barDemo">
    <a lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
    <a href="javascript:void(0)" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
</script>

<!--<script type="text/html" id="shifou">
    <input type="checkbox" name="locked" value="{{d.locked}}" title="启用" lay-filter="roleTable" {{d.locked == 1 ? 'checked' : ''}}>
</script>-->

</body>
</html>